package org.gd136.ce.manager;

import java.io.File;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

/**
 * ResourceManager
 */
public class RM {

	private static RM _instance;

	private static RM instance() {
		if (_instance == null) {
			_instance = new RM();
		}
		return _instance;
	}

	private RM() {
		_init();
	}

	public static void dispose() {
		if (_instance != null) {
			_instance._dispose();
			_instance = null;
		}
	}

	// ////////////////////////////
	Texture colorTexture;
	AssetManager asset;
	FreeTypeFontGenerator generator;
	FreeTypeFontParameter paramter;

	public static FileHandle res(String name) {
		// Desktop
		// if (Gdx.app.getType() == ApplicationType.Desktop) {
		return new FileHandle("data" + File.separatorChar + name);
		// } else {
		// return Gdx.files.internal(name);
		// }
	}

	private void _init() {
		asset = new AssetManager(new FileHandleResolver() {
			@Override
			public FileHandle resolve(String fileName) {
				return res(fileName);
			}
		});
		generator = new FreeTypeFontGenerator(
				res("font/DroidSansFallback.ttf"));
		paramter = new FreeTypeFontParameter();

		// ////
		Pixmap pixmap = new Pixmap(8, 8, Format.RGBA8888);
		pixmap.setColor(Color.WHITE);
		pixmap.fill();
		colorTexture = new Texture(pixmap);
		pixmap.dispose();
		// /////

	}

	private void _dispose() {
		colorTexture.dispose();
		asset.dispose();
		generator.dispose();
	}

	public static AssetManager asset() {
		return instance().asset;
	}

	private Texture _loadGetTexture(String name) {
		if (!res(name).exists()) {
			System.err.println("未找到资源:" + name);
			return colorTexture;
		}

		if (!asset.isLoaded(name, Texture.class)) {
			asset.load(name, Texture.class);
			asset.finishLoadingAsset(name);
		}
		Texture tex = asset.get(name, Texture.class);
		if (tex.getMagFilter() != TextureFilter.Linear) {
			tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		}
		return tex;
	}

	public static Texture loadGetTexture(String name) {
		return instance()._loadGetTexture(name);
	}

	private Sound _loadGetSound(String name) {
		if (!asset.isLoaded(name, Sound.class)) {
			asset.load(name, Sound.class);
			asset.finishLoadingAsset(name);
		}
		return asset.get(name, Sound.class);
	}

	public static Sound loadGetSound(String name) {
		return instance()._loadGetSound(name);
	}

	private Music _loadGetMusic(String name) {
		if (!asset.isLoaded(name, Music.class)) {
			asset.load(name, Music.class);
			asset.finishLoadingAsset(name);
		}
		return asset.get(name, Music.class);
	}

	public static Music loadGetMusic(String name) {
		return instance()._loadGetMusic(name);
	}

	public static Image colorImage() {
		return new Image(instance().colorTexture);
	}

	public static FreeTypeFontGenerator fontGenerator() {
		return instance().generator;
	}

	public static FreeTypeFontParameter parameter() {
		return instance().paramter;
	}

	public static BitmapFont generateFont() {
		return instance().generator.generateFont(instance().paramter);
	}

	public static BitmapFont generateFont(FreeTypeFontParameter paramter) {
		return instance().generator.generateFont(paramter);
	}

}
